//
//  AXFAddressController.m
//  AXFEightGroup
//
//  Created by hgj on 2016/12/22.
//  Copyright © 2016年 ya. All rights reserved.
//

#import "AXFAddressController.h"
#import "AXFAddLocationController.h"
#import "AXFAddressViewCell.h"

static NSString *cell_id = @"cell";

@interface AXFAddressController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) UIView *bottomView;
@property (nonatomic, strong) NSOperationQueue *queue;
@property (nonatomic, strong) NSMutableArray<AXFDataModel *> *addressArray;

@property (nonatomic, strong) AXFDataModel *model;

@end

@implementation AXFAddressController {
    NSString *_title;
}

-(NSOperationQueue *)queue{
    if(!_queue){
        //并发队列
        _queue = [[NSOperationQueue alloc] init];
    }
    return _queue;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    
    self.navigationItem.title = _title;
    
    self.addressArray = [NSMutableArray array];
    
    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        
        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"address3.plist"];
        
        NSMutableArray *mArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            if (mArray) {
                self.addressArray = mArray;
                [self.tableView reloadData];
            }
        }];
    }];
    
    [self.queue addOperation:operation];
    
    [self setupAddLocationView];
    
    [self setupTableView];
}

- (instancetype)initWithTitle:(NSString *)title {
    if (self = [super init]) {
        _title = title;
    }
    return self;
}

#pragma mark - TableView界面 -
- (void)setupTableView {
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    
    [tableView registerClass:[AXFAddressViewCell class] forCellReuseIdentifier:cell_id];
    [tableView registerNib:[UINib nibWithNibName:@"AXFAddressViewCell" bundle:nil] forCellReuseIdentifier:cell_id];
    
    tableView.rowHeight = 60;
    
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_topLayoutGuideBottom);
        make.leading.trailing.equalTo(self.view);
        make.bottom.mas_equalTo(_bottomView.mas_top);
    }];
    _tableView = tableView;
}

#pragma mark - 底部添加地址界面 -
- (void)setupAddLocationView {
    
    //创建
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    
    UIButton *btn = [[UIButton alloc] init];
    [btn setBackgroundImage:[UIImage imageNamed:@"v2_coupon_verify_normal"] forState:UIControlStateNormal];
    [btn setTitle:@"+新增地址" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(pushToAddLocatonViewController:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:btn];
    
    //约束
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(60);
        make.left.bottom.right.mas_equalTo(self.view);
    }];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(bottomView);
        make.width.mas_equalTo(250);
        make.height.mas_equalTo(40);
    }];
    
    _bottomView = bottomView;
}

- (void)pushToAddLocatonViewController:(UIButton *)button {
//    AXFAddLocationController *addVC = [[AXFAddLocationController alloc] init];
    
//    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"v2_goback"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"AXFAddLocationController" bundle:nil];
    AXFAddLocationController *vc = [storyBoard instantiateInitialViewController];
    
    
    vc.Block = ^(AXFDataModel *model) {
        self.model = model;
        [self.addressArray addObject:model];
        [self.tableView reloadData];
    };
    
    [self.navigationController pushViewController:vc animated:YES];
}
//
//- (void)back {
//    [self.navigationController popViewControllerAnimated:YES];
//}

#pragma mark - UITableViewDataSource -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.addressArray == nil) {
        return 0;
    } else {
        return self.addressArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AXFAddressViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id forIndexPath:indexPath];

        AXFDataModel *model = self.addressArray[indexPath.row];
            cell.model = model;
    
    
        NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
    
            NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"address3.plist"];
    
            NSMutableArray *addressArr = [NSMutableArray arrayWithArray:self.addressArray.copy];
            [NSKeyedArchiver archiveRootObject:addressArr toFile:path];
    
        }];
    
        [self.queue addOperation:operation];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

@end
